package com.cskaoyan.controller.promote;

import com.cskaoyan.bean.BaseListData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.model.promote.bo.TopicUpdateBO;
import com.cskaoyan.service.PromoteService;
import com.cskaoyan.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 专题管理
 * 增删改查部分
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    PromoteService promoteService;

    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String sort, String order, String title, String subtitle){
        BaseListData data = promoteService.queryTopics(page, limit, sort, order, title, subtitle);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody TopicUpdateBO topicUpdateBo){
        TopicUpdateBO new_topicUpdateBo = promoteService.updateTopics(topicUpdateBo);
        return BaseRespVo.ok(new_topicUpdateBo);
    }

    /**
     * content 和 price 不能为空
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Map map){
        if( StringUtils.isEmpty((String) map.get("content")) || map.get("price") == null){
            return BaseRespVo.errors();
        }
        TopicUpdateBO data = promoteService.createTopics(map);
        return BaseRespVo.ok(data);

    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        promoteService.deleteTopics(map);
        return BaseRespVo.ok();
    }


}
